package customer

import (
	"context"
	"fmt"
	"sieve_admin_server_userpass/lib/log"
	"sieve_admin_server_userpass/lib/util/errors"
	"strings"
	"sync"
)

var (
	userCronOnce = sync.Once{}
)

func (a *UserSrv) init() error {

	var (
		initCount = 4
		wp        = sync.WaitGroup{}
		logMsg    []string
	)

	wp.Add(initCount)
	go func() {
		err := a.syncUserPoints()
		logMsg = append(logMsg, fmt.Sprintf("%+v", err))
		wp.Done()
	}()

	go func() {
		err := a.syncUserLastLogin()
		logMsg = append(logMsg, fmt.Sprintf("%+v", err))
		wp.Done()
	}()

	go func() {
		err := a.syncUserLastRecharge()
		logMsg = append(logMsg, fmt.Sprintf("%+v", err))
		wp.Done()
	}()

	go func() {
		err := a.syncUserLastConsume()
		logMsg = append(logMsg, fmt.Sprintf("%+v", err))
		wp.Done()
	}()

	wp.Wait()

	return errors.New(strings.Join(logMsg, ","))
}

func (a *UserSrv) Cron() error {

	userCronOnce.Do(func() {
		log.Info(fmt.Sprintf("%v", a.init()))
	})

	return nil
}

// 同步用户的 账号积分消耗，赠送积分消耗数据
func (a *UserSrv) syncUserPoints(args ...interface{}) error {

	var (
		execSQL = fmt.Sprintf(`
UPDATE 
	user
INNER JOIN (
SELECT 
	user_op_record.account_id,
	SUM(user_op_record.points_consume_account) AS points_consume_account,
	SUM(user_op_record.points_consume_gift) AS points_consume_gift
FROM
	user_op_record
WHERE user_op_record.deleted = 0
GROUP BY user_op_record.account_id
) user_consume ON user_consume.account_id = user.id
SET 
	user.points_consume_account = user_consume.points_consume_account,
	user.points_consume_gift = user_consume.points_consume_gift
WHERE user.deleted = 0;
`)
	)

	_, err := a.UserRepo.ExecRaw(context.Background(), execSQL)
	return err
}

// 同步用户的 最近登录时间，最近消耗，最近充值
func (a *UserSrv) syncUserLastLogin(args ...interface{}) error {

	var (
		execSQL = fmt.Sprintf(`
UPDATE user
INNER JOIN (SELECT
    creator,
    MAX(login_time) AS login_time
  FROM login_record
  GROUP BY creator
  WHERE deleted = 0
  ORDER BY id) user_last_login
  ON user_last_login.creator = user.id
SET user.last_login = user_last_login.login_time
WHERE user.deleted = 0;
`)
	)

	_, err := a.UserRepo.ExecRaw(context.Background(), execSQL)
	return err
}

// 同步用户的 最近一次充值
func (a *UserSrv) syncUserLastRecharge(args ...interface{}) error {

	var (
		execSQL = fmt.Sprintf(`
UPDATE user
INNER JOIN (
SELECT
  account_id,
  MAX(updated_at) AS last_recharge
FROM recharge
WHERE deleted = 0
GROUP BY account_id
ORDER BY id
) user_last_recharge ON user_last_recharge.account_id = user.id
SET user.last_recharge = user_last_recharge.last_recharge
WHERE user.deleted = 0 AND user.role_id = %v;
`, a.RoleSrv.GetChannelAgentRole().ID)
	)

	_, err := a.UserRepo.ExecRaw(context.Background(), execSQL)
	return err
}

// 同步用户的 最近一次消耗
func (a *UserSrv) syncUserLastConsume(args ...interface{}) error {

	var (
		execSQL = fmt.Sprintf(`
UPDATE user
INNER JOIN (
SELECT
  account_id,
  MAX(updated_at) AS last_consume
FROM user_op_record
GROUP BY account_id
ORDER BY id
) user_last_consume ON user_last_consume.account_id = user.id
SET user.last_consume = user_last_consume.last_consume
WHERE user.deleted = 0 AND user.role_id = %v;
`, a.RoleSrv.GetChannelAgentRole().ID)
	)

	_, err := a.UserRepo.ExecRaw(context.Background(), execSQL)
	return err
}
